const state = {
    visitedViews: [],
    cachedViews: []
}

const mutations = {
    ADD_VISITED_VIEW: (state, view) => {
        // 添加调试日志
        console.log('添加标签视图:', {
            path: view.path,
            name: view.name,
            meta: view.meta,
            fullPath: view.fullPath
        })

        // 检查view是否有效
        if (!view || !view.path) {
            console.error('添加的视图无效:', view)
            return
        }

        // 检查是否已存在相同路径的标签
        if (state.visitedViews.some(v => v.path === view.path)) {
            // 更新现有标签
            for (let i = 0; i < state.visitedViews.length; i++) {
                if (state.visitedViews[i].path === view.path) {
                    // 合并query和params
                    state.visitedViews[i] = {
                        ...state.visitedViews[i],
                        query: view.query || state.visitedViews[i].query,
                        params: view.params || state.visitedViews[i].params,
                        fullPath: view.fullPath || state.visitedViews[i].fullPath,
                        title: view.meta?.title || state.visitedViews[i].title || 'no-name'
                    }
                    console.log('更新已有标签:', state.visitedViews[i])
                    return
                }
            }
            return
        }

        // 添加新标签
        const newTag = Object.assign({}, view, {
            title: view.meta?.title || 'no-name'
        })
        state.visitedViews.push(newTag)
        console.log('添加新标签成功:', newTag)
        console.log('当前标签列表:', state.visitedViews.map(v => ({ path: v.path, title: v.title })))
    },

    ADD_CACHED_VIEW: (state, view) => {
        // 添加调试日志
        console.log('添加缓存视图:', { name: view.name, meta: view.meta })

        if (!view.name) {
            console.warn('视图没有name属性，无法缓存:', view)
            return
        }

        if (state.cachedViews.includes(view.name)) return
        if (!view.meta?.noCache) {
            state.cachedViews.push(view.name)
            console.log('缓存视图添加成功:', view.name)
            console.log('当前缓存列表:', state.cachedViews)
        }
    },

    DEL_VISITED_VIEW: (state, view) => {
        for (const [i, v] of state.visitedViews.entries()) {
            if (v.path === view.path) {
                state.visitedViews.splice(i, 1)
                break
            }
        }
    },

    DEL_CACHED_VIEW: (state, view) => {
        if (!view.name) return
        const index = state.cachedViews.indexOf(view.name)
        if (index > -1) {
            state.cachedViews.splice(index, 1)
        }
    },

    DEL_OTHERS_VISITED_VIEWS: (state, view) => {
        state.visitedViews = state.visitedViews.filter(v => {
            return v.meta.affix || v.path === view.path
        })
    },

    DEL_OTHERS_CACHED_VIEWS: (state, view) => {
        if (!view.name) return
        const index = state.cachedViews.indexOf(view.name)
        if (index > -1) {
            state.cachedViews = state.cachedViews.slice(index, index + 1)
        } else {
            // 如果当前视图不在缓存中，则清空所有缓存
            state.cachedViews = []
        }
    },

    DEL_ALL_VISITED_VIEWS: state => {
        // 保留固定的标签
        state.visitedViews = state.visitedViews.filter(tag => tag.meta && tag.meta.affix)
    },

    DEL_ALL_CACHED_VIEWS: state => {
        state.cachedViews = []
    },

    UPDATE_VISITED_VIEW: (state, view) => {
        for (let i = 0; i < state.visitedViews.length; i++) {
            if (state.visitedViews[i].path === view.path) {
                state.visitedViews[i] = {
                    ...state.visitedViews[i],
                    query: view.query || state.visitedViews[i].query,
                    params: view.params || state.visitedViews[i].params,
                    fullPath: view.fullPath || state.visitedViews[i].fullPath,
                    title: view.meta.title || state.visitedViews[i].title || 'no-name'
                }
                break
            }
        }
    },

    // 修复标签顺序问题
    REORDER_VISITED_VIEW: (state, { oldIndex, newIndex }) => {
        const viewTemp = state.visitedViews[oldIndex]
        state.visitedViews.splice(oldIndex, 1)
        state.visitedViews.splice(newIndex, 0, viewTemp)
    }
}

const actions = {
    // 添加标签视图
    addView({ dispatch }, view) {
        return new Promise(resolve => {
            dispatch('addVisitedView', view)
            dispatch('addCachedView', view)
            resolve()
        })
    },

    // 添加访问过的视图
    addVisitedView({ commit }, view) {
        return new Promise(resolve => {
            commit('ADD_VISITED_VIEW', view)
            resolve()
        })
    },

    // 添加缓存视图
    addCachedView({ commit }, view) {
        return new Promise(resolve => {
            commit('ADD_CACHED_VIEW', view)
            resolve()
        })
    },

    // 删除标签视图
    delView({ dispatch, state }, view) {
        return new Promise(resolve => {
            dispatch('delVisitedView', view)
            dispatch('delCachedView', view)
            resolve({
                visitedViews: [...state.visitedViews],
                cachedViews: [...state.cachedViews]
            })
        })
    },

    // 删除访问过的视图
    delVisitedView({ commit, state }, view) {
        return new Promise(resolve => {
            commit('DEL_VISITED_VIEW', view)
            resolve([...state.visitedViews])
        })
    },

    // 删除缓存视图
    delCachedView({ commit, state }, view) {
        return new Promise(resolve => {
            commit('DEL_CACHED_VIEW', view)
            resolve([...state.cachedViews])
        })
    },

    // 删除其他视图
    delOthersViews({ dispatch, state }, view) {
        return new Promise(resolve => {
            dispatch('delOthersVisitedViews', view)
            dispatch('delOthersCachedViews', view)
            resolve({
                visitedViews: [...state.visitedViews],
                cachedViews: [...state.cachedViews]
            })
        })
    },

    // 删除其他访问过的视图
    delOthersVisitedViews({ commit, state }, view) {
        return new Promise(resolve => {
            commit('DEL_OTHERS_VISITED_VIEWS', view)
            resolve([...state.visitedViews])
        })
    },

    // 删除其他缓存视图
    delOthersCachedViews({ commit, state }, view) {
        return new Promise(resolve => {
            commit('DEL_OTHERS_CACHED_VIEWS', view)
            resolve([...state.cachedViews])
        })
    },

    // 删除所有视图
    delAllViews({ dispatch, state }) {
        return new Promise(resolve => {
            dispatch('delAllVisitedViews')
            dispatch('delAllCachedViews')
            resolve({
                visitedViews: [...state.visitedViews],
                cachedViews: [...state.cachedViews]
            })
        })
    },

    // 删除所有访问过的视图
    delAllVisitedViews({ commit, state }) {
        return new Promise(resolve => {
            commit('DEL_ALL_VISITED_VIEWS')
            resolve([...state.visitedViews])
        })
    },

    // 删除所有缓存视图
    delAllCachedViews({ commit, state }) {
        return new Promise(resolve => {
            commit('DEL_ALL_CACHED_VIEWS')
            resolve([...state.cachedViews])
        })
    },

    // 更新视图
    updateVisitedView({ commit }, view) {
        return new Promise(resolve => {
            commit('UPDATE_VISITED_VIEW', view)
            resolve()
        })
    },

    // 重新排序标签
    reorderVisitedView({ commit }, { oldIndex, newIndex }) {
        return new Promise(resolve => {
            commit('REORDER_VISITED_VIEW', { oldIndex, newIndex })
            resolve()
        })
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
} 